﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CreateContinuousMFGFromCurve.CApCMCCreateContinuousMFGFromCurveLogic
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.DataTypes;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.CreateContinuousMFGFromCurve;

internal abstract class CApCMCCreateContinuousMFGFromCurveLogic
{
  protected TxObjGridCtrl _mfgList;
  protected TxObjEditBoxCtrl _assignToPart;
  protected ComboBox _mfgType;
  protected Button _okButton;
  protected string _CONTINUOUS_MFG_METADATA_TYPE = "";
  private const string _PLANNING_TYPE_REGISTRY = "ContinuousMfgPlanningType";
  private const string _STRING_TABLE_LOCATION = "DnProcessSimulateCommands.CreateContinuousMFGFromCurve.CMCStringTable";
  private const string _PRESERVED_ATTRIBUTE = "Preserved";
  protected ResourceManager _resourceManager;
  private Dictionary<string, string> _mfgTypesDisplayNames;
  protected Func<ITxSeamMfgFeatureCreationData, ITxSeamMfgFeatureCreationData> ModifyMfgData;
  protected Func<ITxObject, ITxSeamMfgFeatureCreationData, TxSeamMfgFeature> CallCreateSeamMfg;

  public CApCMCCreateContinuousMFGFromCurveLogic(
    TxObjGridCtrl mfgList,
    TxObjEditBoxCtrl assignToPart,
    ComboBox mfgType,
    Button okButton,
    string continuousMfgMetaDataType)
  {
    this._mfgList = mfgList;
    this._assignToPart = assignToPart;
    this._mfgType = mfgType;
    this._okButton = okButton;
    this._resourceManager = new ResourceManager("DnProcessSimulateCommands.CreateContinuousMFGFromCurve.CMCStringTable", this.GetType().Assembly);
    this._CONTINUOUS_MFG_METADATA_TYPE = continuousMfgMetaDataType;
    this.FillPlaningTypeList();
  }

  private void FillPlaningTypeList()
  {
    this._mfgTypesDisplayNames = new Dictionary<string, string>();
    TxPlanningTypeMetaData typeMetaData = TxApplication.ActiveDocument.PlatformGlobalServicesProvider.GetTypeMetaData(this._CONTINUOUS_MFG_METADATA_TYPE);
    if (typeMetaData == null || typeMetaData.DerivedTypes.Count <= 0)
      return;
    foreach (TxPlanningTypeMetaData derivedType in typeMetaData.DerivedTypes)
    {
      if (derivedType.TypeName != this._CONTINUOUS_MFG_METADATA_TYPE)
        this._mfgType.Items.Add((object) derivedType.DisplayName);
      else
        this._mfgType.Items.Insert(0, (object) derivedType.DisplayName);
      this._mfgTypesDisplayNames.Add(derivedType.DisplayName, derivedType.TypeName);
    }
  }

  public int GetPlanningTypeIndexFromRegistry(Form form)
  {
    object obj = (object) null;
    string empty = string.Empty;
    int indexFromRegistry = 0;
    TxFormSettings.RestoreFormValue(form, "ContinuousMfgPlanningType", ref obj);
    if (obj != null)
    {
      string key = obj.ToString();
      if (!string.IsNullOrEmpty(key) && this._mfgTypesDisplayNames.ContainsKey(key))
        indexFromRegistry = this._mfgType.Items.IndexOf((object) key);
    }
    if (indexFromRegistry < 0)
      indexFromRegistry = 0;
    return indexFromRegistry;
  }

  public void SetPlanningTypeInRegistry(Form form)
  {
    TxFormSettings.StoreFormValue(form, "ContinuousMfgPlanningType", (object) this._mfgType.Text);
  }

  public void CreateMFGs() => this.CreateMFGs(string.Empty);

  public string GetMfgTypeByMfgTypeDisplayName(string mfgTypeDisplayName)
  {
    string mfgTypeDisplayName1 = (string) null;
    this._mfgTypesDisplayNames?.TryGetValue(mfgTypeDisplayName, out mfgTypeDisplayName1);
    return mfgTypeDisplayName1;
  }

  public virtual List<TxSeamMfgFeature> CreateMFGs(string mfgTypeName)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    this._mfgList.LoseFocus();
    List<TxSeamMfgFeature> mfGs = new List<TxSeamMfgFeature>();
    List<string> stringList = new List<string>();
    List<TxDisplayableObjectVisibility> objectVisibilityList = new List<TxDisplayableObjectVisibility>();
    TxApplication.ActiveUndoManager.StartTransaction();
    for (int index = 0; index < this._mfgList.Count; ++index)
      objectVisibilityList.Add((this._mfgList.GetObject(index) as ITxDisplayableObject).Visibility);
    for (int index = 0; index < this._mfgList.Count; ++index)
    {
      string textValue = this._mfgList.GetTextValue(index, 0);
      ITxObject itxObject = this._mfgList.GetObject(index);
      itxObject.RemoveAttribute("Preserved");
      ITxSeamMfgFeatureCreationData featureCreationData = TxMfgCreationDataFactory.CreateSeamMfgFeatureCreationData(textValue);
      if (!string.IsNullOrEmpty(mfgTypeName) && this._mfgTypesDisplayNames.ContainsKey(mfgTypeName))
        featureCreationData.PlanningType = this._mfgTypesDisplayNames[mfgTypeName];
      if (this.ModifyMfgData != null)
        featureCreationData = this.ModifyMfgData(featureCreationData);
      ((ITxLocatableObjectCreationData) featureCreationData).AbsoluteLocation = (itxObject as ITxLocatableObject).GeometricCenter;
      (itxObject as ITxDisplayableObject).Display();
      TxSeamMfgFeature seam = this.CallCreateSeamMfg == null ? TxApplication.ActiveDocument.MfgRoot.CreateSeamMfgFeature(itxObject, featureCreationData, "") : this.CallCreateSeamMfg(itxObject, featureCreationData);
      if (TxSeamMfgFeature.op_Inequality(seam, (ITxObject) null))
      {
        this.AssignAndAttachToPart(seam);
        seam.StartPoint = ((TxMfgFeature) seam).AbsoluteLocation.Inverse;
        seam.EndPoint = ((TxMfgFeature) seam).AbsoluteLocation.Inverse;
        seam.IntermediatePoint = ((TxMfgFeature) seam).AbsoluteLocation.Inverse;
      }
      else
        stringList.Add(textValue);
      if (objectVisibilityList[index] == 2)
        (itxObject as ITxDisplayableObject).Blank();
      mfGs.Add(seam);
    }
    TxApplication.ActiveUndoManager.EndTransaction();
    Cursor.Current = current;
    if (stringList.Count > 0)
    {
      StringBuilder stringBuilder = new StringBuilder(this._resourceManager.GetString("CMC_MFGCreationFailed"));
      string str1 = this._resourceManager.GetString("CMC_ErrorTitle");
      foreach (string str2 in stringList)
      {
        stringBuilder.Append("\n");
        stringBuilder.Append(str2);
      }
      int num = (int) TxMessageBox.Show(stringBuilder.ToString(), str1, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    return mfGs;
  }

  private void AssignAndAttachToPart(TxSeamMfgFeature seam)
  {
    if (this._assignToPart.Object == null)
      return;
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(this._assignToPart.Object);
    ((TxMfgFeature) seam).ClearAssignedParts();
    ((TxMfgFeature) seam).AssignParts(txObjectList);
    seam.LeadingPart = ((Collection<ITxObject>) txObjectList)[0] as ITxComponent;
    ITxLocatableObject itxLocatableObject1 = ((Collection<ITxObject>) txObjectList)[0] as ITxLocatableObject;
    ITxLocatableObject itxLocatableObject2 = (ITxLocatableObject) seam;
    if (itxLocatableObject1 == null || itxLocatableObject2 == null || !itxLocatableObject2.CanAttachTo(itxLocatableObject1))
      return;
    itxLocatableObject2.AttachTo(itxLocatableObject1);
  }
}
